{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "d92b1e17-23db-49ac-accc-ab76b373359b",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(491, 5)\n",
      "(491, 4)\n",
      "(123, 5)\n",
      "(123, 4)\n",
      "Epoch 1/50\n",
      "123/123 [==============================] - 1s 3ms/step - loss: 1.1927 - accuracy: 0.5377 - val_loss: 1.0019 - val_accuracy: 0.6098\n",
      "Epoch 2/50\n",
      "123/123 [==============================] - 0s 2ms/step - loss: 0.8942 - accuracy: 0.6578 - val_loss: 0.8378 - val_accuracy: 0.6748\n",
      "Epoch 3/50\n",
      "123/123 [==============================] - 0s 2ms/step - loss: 0.7918 - accuracy: 0.7169 - val_loss: 0.8066 - val_accuracy: 0.7317\n",
      "Epoch 4/50\n",
      "123/123 [==============================] - 0s 2ms/step - loss: 0.7634 - accuracy: 0.7149 - val_loss: 0.7777 - val_accuracy: 0.7724\n",
      "Epoch 5/50\n",
      "123/123 [==============================] - 0s 2ms/step - loss: 0.6497 - accuracy: 0.7475 - val_loss: 0.6851 - val_accuracy: 0.7805\n",
      "Epoch 6/50\n",
      "123/123 [==============================] - 0s 2ms/step - loss: 0.5678 - accuracy: 0.7862 - val_loss: 0.7338 - val_accuracy: 0.7073\n",
      "Epoch 7/50\n",
      "123/123 [==============================] - 0s 2ms/step - loss: 0.5677 - accuracy: 0.7760 - val_loss: 0.7316 - val_accuracy: 0.7398\n",
      "Epoch 8/50\n",
      "123/123 [==============================] - 0s 2ms/step - loss: 0.5081 - accuracy: 0.7821 - val_loss: 0.6706 - val_accuracy: 0.8049\n",
      "Epoch 9/50\n",
      "123/123 [==============================] - 0s 2ms/step - loss: 0.4970 - accuracy: 0.8065 - val_loss: 0.6349 - val_accuracy: 0.7967\n",
      "Epoch 10/50\n",
      "123/123 [==============================] - 0s 2ms/step - loss: 0.4505 - accuracy: 0.8086 - val_loss: 0.6674 - val_accuracy: 0.7561\n",
      "Epoch 11/50\n",
      "123/123 [==============================] - 0s 2ms/step - loss: 0.4349 - accuracy: 0.8167 - val_loss: 0.6041 - val_accuracy: 0.7724\n",
      "Epoch 12/50\n",
      "123/123 [==============================] - 0s 2ms/step - loss: 0.4048 - accuracy: 0.8574 - val_loss: 0.6573 - val_accuracy: 0.7724\n",
      "Epoch 13/50\n",
      "123/123 [==============================] - 0s 2ms/step - loss: 0.4182 - accuracy: 0.8330 - val_loss: 0.5775 - val_accuracy: 0.8130\n",
      "Epoch 14/50\n",
      "123/123 [==============================] - 0s 2ms/step - loss: 0.3703 - accuracy: 0.8697 - val_loss: 0.6113 - val_accuracy: 0.7642\n",
      "Epoch 15/50\n",
      "123/123 [==============================] - 0s 2ms/step - loss: 0.3530 - accuracy: 0.8595 - val_loss: 0.4988 - val_accuracy: 0.8049\n",
      "Epoch 16/50\n",
      "123/123 [==============================] - 0s 2ms/step - loss: 0.3365 - accuracy: 0.8717 - val_loss: 0.4547 - val_accuracy: 0.8211\n",
      "Epoch 17/50\n",
      "123/123 [==============================] - 0s 2ms/step - loss: 0.3356 - accuracy: 0.8778 - val_loss: 0.4886 - val_accuracy: 0.8049\n",
      "Epoch 18/50\n",
      "123/123 [==============================] - 0s 2ms/step - loss: 0.3248 - accuracy: 0.8859 - val_loss: 0.4380 - val_accuracy: 0.8374\n",
      "Epoch 19/50\n",
      "123/123 [==============================] - 0s 2ms/step - loss: 0.3252 - accuracy: 0.8758 - val_loss: 0.4460 - val_accuracy: 0.8374\n",
      "Epoch 20/50\n",
      "123/123 [==============================] - 0s 2ms/step - loss: 0.2869 - accuracy: 0.8941 - val_loss: 0.5302 - val_accuracy: 0.7967\n",
      "Epoch 21/50\n",
      "123/123 [==============================] - 0s 2ms/step - loss: 0.2751 - accuracy: 0.9002 - val_loss: 0.4269 - val_accuracy: 0.8537\n",
      "Epoch 22/50\n",
      "123/123 [==============================] - 0s 2ms/step - loss: 0.2962 - accuracy: 0.8839 - val_loss: 0.6445 - val_accuracy: 0.7317\n",
      "Epoch 23/50\n",
      "123/123 [==============================] - 0s 2ms/step - loss: 0.2896 - accuracy: 0.8859 - val_loss: 0.3899 - val_accuracy: 0.8618\n",
      "Epoch 24/50\n",
      "123/123 [==============================] - 0s 2ms/step - loss: 0.2428 - accuracy: 0.9063 - val_loss: 0.3889 - val_accuracy: 0.8699\n",
      "Epoch 25/50\n",
      "123/123 [==============================] - 0s 2ms/step - loss: 0.2306 - accuracy: 0.9206 - val_loss: 0.3622 - val_accuracy: 0.8780\n",
      "Epoch 26/50\n",
      "123/123 [==============================] - 0s 2ms/step - loss: 0.2458 - accuracy: 0.9226 - val_loss: 0.4423 - val_accuracy: 0.8455\n",
      "Epoch 27/50\n",
      "123/123 [==============================] - 0s 2ms/step - loss: 0.2249 - accuracy: 0.9063 - val_loss: 0.4728 - val_accuracy: 0.8455\n",
      "Epoch 28/50\n",
      "123/123 [==============================] - 0s 2ms/step - loss: 0.2155 - accuracy: 0.9145 - val_loss: 0.4520 - val_accuracy: 0.8618\n",
      "Epoch 29/50\n",
      "123/123 [==============================] - 0s 2ms/step - loss: 0.2271 - accuracy: 0.9246 - val_loss: 0.3604 - val_accuracy: 0.8699\n",
      "Epoch 30/50\n",
      "123/123 [==============================] - 0s 2ms/step - loss: 0.2021 - accuracy: 0.9369 - val_loss: 0.3115 - val_accuracy: 0.8862\n",
      "Epoch 31/50\n",
      "123/123 [==============================] - 0s 2ms/step - loss: 0.2024 - accuracy: 0.9165 - val_loss: 0.5307 - val_accuracy: 0.7724\n",
      "Epoch 32/50\n",
      "123/123 [==============================] - 0s 2ms/step - loss: 0.1937 - accuracy: 0.9308 - val_loss: 0.3790 - val_accuracy: 0.8862\n",
      "Epoch 33/50\n",
      "123/123 [==============================] - 0s 2ms/step - loss: 0.1880 - accuracy: 0.9348 - val_loss: 0.4365 - val_accuracy: 0.8455\n",
      "Epoch 34/50\n",
      "123/123 [==============================] - 0s 2ms/step - loss: 0.2048 - accuracy: 0.9287 - val_loss: 0.3822 - val_accuracy: 0.8699\n",
      "Epoch 35/50\n",
      "123/123 [==============================] - 0s 2ms/step - loss: 0.1473 - accuracy: 0.9572 - val_loss: 0.3289 - val_accuracy: 0.9268\n",
      "Epoch 36/50\n",
      "123/123 [==============================] - 0s 2ms/step - loss: 0.1527 - accuracy: 0.9409 - val_loss: 0.3106 - val_accuracy: 0.9106\n",
      "Epoch 37/50\n",
      "123/123 [==============================] - 0s 2ms/step - loss: 0.1475 - accuracy: 0.9409 - val_loss: 0.3647 - val_accuracy: 0.9187\n",
      "Epoch 38/50\n",
      "123/123 [==============================] - 0s 2ms/step - loss: 0.1394 - accuracy: 0.9552 - val_loss: 0.5855 - val_accuracy: 0.8699\n",
      "Epoch 39/50\n",
      "123/123 [==============================] - 0s 2ms/step - loss: 0.1823 - accuracy: 0.9389 - val_loss: 0.2945 - val_accuracy: 0.9350\n",
      "Epoch 40/50\n",
      "123/123 [==============================] - 0s 2ms/step - loss: 0.1277 - accuracy: 0.9491 - val_loss: 0.2990 - val_accuracy: 0.8943\n",
      "Epoch 41/50\n",
      "123/123 [==============================] - 0s 2ms/step - loss: 0.1689 - accuracy: 0.9430 - val_loss: 0.4421 - val_accuracy: 0.8699\n",
      "Epoch 42/50\n",
      "123/123 [==============================] - 0s 2ms/step - loss: 0.1725 - accuracy: 0.9348 - val_loss: 0.3635 - val_accuracy: 0.9187\n",
      "Epoch 43/50\n",
      "123/123 [==============================] - 0s 2ms/step - loss: 0.1485 - accuracy: 0.9450 - val_loss: 0.3569 - val_accuracy: 0.9024\n",
      "Epoch 44/50\n",
      "123/123 [==============================] - 0s 2ms/step - loss: 0.1266 - accuracy: 0.9572 - val_loss: 0.3151 - val_accuracy: 0.9512\n",
      "Epoch 45/50\n",
      "123/123 [==============================] - 0s 2ms/step - loss: 0.1253 - accuracy: 0.9491 - val_loss: 0.2984 - val_accuracy: 0.9512\n",
      "Epoch 46/50\n",
      "123/123 [==============================] - 0s 2ms/step - loss: 0.1392 - accuracy: 0.9532 - val_loss: 0.3121 - val_accuracy: 0.9350\n",
      "Epoch 47/50\n",
      "123/123 [==============================] - 0s 2ms/step - loss: 0.1320 - accuracy: 0.9613 - val_loss: 0.3743 - val_accuracy: 0.8943\n",
      "Epoch 48/50\n",
      "123/123 [==============================] - 0s 2ms/step - loss: 0.2178 - accuracy: 0.9430 - val_loss: 0.3443 - val_accuracy: 0.8943\n",
      "Epoch 49/50\n",
      "123/123 [==============================] - 0s 2ms/step - loss: 0.1311 - accuracy: 0.9613 - val_loss: 0.3007 - val_accuracy: 0.9268\n",
      "Epoch 50/50\n",
      "123/123 [==============================] - 0s 2ms/step - loss: 0.1256 - accuracy: 0.9430 - val_loss: 0.2766 - val_accuracy: 0.9512\n",
      "4/4 [==============================] - 0s 2ms/step - loss: 0.2766 - accuracy: 0.9512\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "from autopilot_data import AutopilotData\n",
    "# from sklearn.model_selection import train_test_split\n",
    "\n",
    "input_num = 5 #输入值个数\n",
    "classes_num = 4 #分类个数\n",
    "input_shape = (input_num)\n",
    "\n",
    "ad = AutopilotData()\n",
    "x,y = ad.load_data()\n",
    "\n",
    "x_train,y_train,x_test,y_test = ad.split_data(x,y,train_rate=0.8)\n",
    "\n",
    "print(x_train.shape)\n",
    "print(y_train.shape)\n",
    "print(x_test.shape)\n",
    "print(y_test.shape)\n",
    "\n",
    "x_train, x_test = x_train / 500.0, x_test / 500.0\n",
    "y_train, y_test = y_train / 1.0, y_test / 1.0\n",
    "\n",
    "model = tf.keras.models.Sequential()\n",
    "model.add(tf.keras.layers.Dense(1024,input_shape=(5,), activation='relu'))\n",
    "model.add(tf.keras.layers.Dense(128, activation='relu'))\n",
    "model.add(tf.keras.layers.Dense(32, activation='relu'))\n",
    "model.add(tf.keras.layers.Dense(classes_num, activation='softmax'))\n",
    "\n",
    "model.compile(optimizer='adam',\n",
    "              loss='categorical_crossentropy',\n",
    "              metrics=['accuracy'])\n",
    "callbacks = [tf.keras.callbacks.TensorBoard('./keras')]\n",
    "model.fit(x_train, y_train, epochs=50, batch_size=4, verbose=1, validation_data=(x_test, y_test), callbacks=callbacks)\n",
    "model.evaluate(x_test, y_test, batch_size=None, verbose=1, sample_weight=None, steps=None,callbacks=None, max_queue_size=10, workers=1, use_multiprocessing=False,return_dict=False)\n",
    "\n",
    "model.save('model.h5')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e222c963-72f9-4afc-9e61-438c03102a0e",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7321727f-cd45-4b80-b088-5ea9aefb01ed",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "adeab07c-cc1f-4882-a6fa-ab233f6085cb",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.15"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
